package cn.itcast.health.provider.service.impl;

import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.commons.utils.DateUtils;
import cn.itcast.health.interfaces.service.OrdersettingService;
import cn.itcast.health.mapper.OrdersettingMapper;
import cn.itcast.health.pojo.OrderSetting;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    public Result importOrderSettings(List<String[]> filelist) {
        Result result = null;
        try {
            //检查文件
            if(filelist != null){
                List<OrderSetting> orderSettings = new ArrayList<>();
                if(filelist.size() > 0){
                    for (String[] strings : filelist) {
//                        System.out.println(strings[0]);
                        if(strings.length > 2){
                            orderSettings.add( new OrderSetting(DateUtils.parseString2Date(strings[0],"yyyy-MM-dd"),Integer.parseInt(strings[1]),Integer.parseInt(strings[2])) );
                        }else {
                            orderSettings.add( new OrderSetting(DateUtils.parseString2Date(strings[0],"yyyy-MM-dd"),Integer.parseInt(strings[1])) );
                        }
                    }
                }
                ordersettingMapper.importOrderSettings(orderSettings);
                result = new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
            }
        } catch (Exception e) {
            result = new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return result;
    }

    @Override
    public Result findOrderSettingByDate(String year, String month) {
        Result result = null;
        try{
            String beginDate = year+"-"+month+"-1";
            String endDate = year+"-"+month+"-31";
            List<Map> maps  = ordersettingMapper.findOrderSettingByDate(beginDate,endDate);
            result = new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS,maps);
        }catch (Exception e){
            result = new Result(false,MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
        return result;
    }

    @Override
    public Result updateNumbersByDate(Map map) {
        Result result = null;
        try{
            String date = (String) map.get("date");
            String  count = (String) map.get("count");
            ordersettingMapper.updateNumbersByDate(date,count);
            result = new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        }catch (Exception e){
            result = new Result(false,MessageConstant.EDIT_ORDERSETTING_FAIL);
        }
        return result;
}
}
